import java.util.Arrays;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/18 13:28
 * @description:
 */
public class Solution135 {
    public int candy(int[] ratings) {
        if (ratings == null || ratings.length == 0) {
            return 0;
        }
        int n = ratings.length;
        int[] candies = new int[n];
        Arrays.fill(candies, 1);
        for (int i = 1; i < n; i++) {
            if (ratings[i] > ratings[i - 1]) {
                candies[i] = candies[i - 1] + 1;
            }
        }
        for (int i = n - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                candies[i] = Math.max(candies[i], candies[i + 1] + 1);
            }
        }
        int count = 0;
        for (int i = 0; i < candies.length; i++) {
            count += candies[i];
        }
        return count;
    }

    public static void main(String[] args) {
        Solution135 solution = new Solution135();
        System.out.println(solution.candy(new int[]{1, 0, 2}));
        System.out.println(solution.candy(new int[]{1, 2, 2}));
        System.out.println(solution.candy(new int[]{1, 3, 2, 2, 1}));
    }
}
